package com.abou.mvc.validation.impl;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import com.abou.mvc.validation.annotation.DoubleValid;

/**
 * The validator used to check that a product object contains consistent values
 * 
 * @author khalid
 */
public class DoubleValidConstraintValidator implements
    ConstraintValidator<DoubleValid, String> {
  
  /**
   * @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
   */
  public void initialize(DoubleValid annotation) {

  }
  
  /**
   * @see javax.validation.ConstraintValidator#isValid(java.lang.Object,
   *      javax.validation.ConstraintValidatorContext)
   */
  public boolean isValid(String stringDouble, ConstraintValidatorContext context) {

    boolean isValid = true;
    
    try {
      
      Double.parseDouble(stringDouble);
      
    } catch (NumberFormatException e) {
      isValid = false;
    }
    return isValid;
  }
}
